from __future__ import division

# TODO

'''
The following info needs to be varified.
18 011 231215 715577494 723677712
19 0120j4 0 -1 -1
20 014 206801 723678244 730924554
21 015 247034 730925023 739582529
'''

'''
some useful functions I can use
inputProcessingDataFileHandler.tell()
inputProcessingDataFileHandler.seek()
'''


# This part is used for verification
# option1 for the machine pangolin
# inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/rawTermInvertedIndexOutput_with_trecID_added.txt"
# option2 for the machine dodo
inputFileName = "/home/diaosi/gov2ClearYourMindAndDoItAgain/rawTermInvertedIndexOutput_with_trecID_added.txt"
inputFileHandler = open(inputFileName,"r")

inputFileHandler.seek(80402426742)
# my imagination
for i in range(0,30):
    tryLine = inputFileHandler.readline()
    print i,tryLine,

inputFileHandler.seek(80404202899)
tryLine = inputFileHandler.readline()
print tryLine,

inputFileHandler.close()


'''
# This part is used for building the structure
# The file dealing with time is:
inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/rawTermInvertedIndexOutput_with_trecID_added.txt"
inputFileHandler = open(inputFileName,"r")

# in production mode
outputFileName = "/data5/team/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/rawTermInvertedIndexOutputAccessAuxFile_NEW_20121128.txt"

# in testing mode
# outputFileName = "/data5/team/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/rawTermInvertedIndexOutputAccessAuxFile_test.txt"

outputFileHandler = open(outputFileName,"w")

currentLine = inputFileHandler.readline()

while currentLine:
    if currentLine.startswith("qid:"):
        print currentLine.strip()
        currentQueryQid = currentLine.strip().split(":")[1].strip()
        
        currentLine = inputFileHandler.readline()
        currentLine = inputFileHandler.readline()
        if currentLine.startswith("Search:"):
            #print currentLine.strip()
            currentQueryLexiconTerm = currentLine.strip().split(" ")[1].strip() 
            print "currentQueryLexiconTerm:",currentQueryLexiconTerm
            currentLine = inputFileHandler.readline()
            nextLine = inputFileHandler.readline()
            
            if len( nextLine.strip().split(" ") ) == 3:
                # There are some posting entries there
                currentInvertedIndexBeginningPosition = inputFileHandler.tell() - len(nextLine)
                while not currentLine.startswith("total_num_of_postings:"):
                    previousLine = currentLine
                    currentLine = inputFileHandler.readline()
                
                currentQueryLexiconLengthOfInvertedIndex = currentLine.strip().split(":")[1].strip()
                currentInvertedIndexEndingPosition = inputFileHandler.tell() - len(currentLine) - len(previousLine)
                outputLine = currentQueryQid + " " + currentQueryLexiconTerm + " " + str(currentQueryLexiconLengthOfInvertedIndex) + " " + str(currentInvertedIndexBeginningPosition) + " " + str(currentInvertedIndexEndingPosition)
                outputFileHandler.write(outputLine + "\n")
                print outputLine
            else:
                # This query returns 0 results from the IR system
                currentQueryLexiconLengthOfInvertedIndex = 0
                currentInvertedIndexBeginningPosition = -1
                currentInvertedIndexEndingPosition = -1
                outputLine = currentQueryQid + " " + currentQueryLexiconTerm + " " + str(currentQueryLexiconLengthOfInvertedIndex) + " " + str(currentInvertedIndexBeginningPosition) + " " + str(currentInvertedIndexEndingPosition)
                outputFileHandler.write(outputLine + "\n")
                print outputLine
        else:
            print "input file format, ERROR"
        print
    
    currentLine = inputFileHandler.readline()

inputFileHandler.close()
outputFileHandler.close()
'''
